﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

public partial class PropertyList : System.Web.UI.UserControl
{
    private SPList list;
    private string propertyID = "1";

    protected DataTable ViewingAppointmentCollection = new DataTable();

    private string editPageUrl = "";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("Estate Agents")]
    public string EditPageUrl
    {
        get { return editPageUrl; }
        set { editPageUrl = value; }
    }

    private string newPageUrl = "";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("Estate Agents")]
    public string NewPageUrl
    {
        get { return newPageUrl; }
        set { newPageUrl = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        propertyID = HttpContext.Current.Request.QueryString["id"];

        /* used in a test envrionment*/

        using (SPSite site = new SPSite("http://yu-pc:33333/sites/Home"))
        {
            using (SPWeb web = site.AllWebs[0])
            {
                string listName = "ViewingAppointments";
                list = web.Lists[listName];
                BindData(list, "");    
            }
        }
    
        /* used in sharepoint context */
        /*
        if (string.IsNullOrEmpty(PropertyListName)) return;
     
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = SPControl.GetContextSite(Context).OpenWeb())
            {

                string listName = PropertyListName;
                propertyList = web.Lists[listName];
                BindData(propertyList, "");
            }
        }
     

        this.AddPropertyHyperLink.NavigateUrl = this.AddPropertyPageUrl;
         * */
    }

    private DataTable SearchProperties(SPList list)
    {
        SPQuery qry = new SPQuery();

        propertyID = "1";

        if (!string.IsNullOrEmpty(propertyID))
        {
            qry.Query = "<Where><Eq><FieldRef Name='Property' LookupId='true'  /><Value Type='Lookup'>1</Value></Eq></Where>";
          
        }

        else return new DataTable();

        DataTable dataTable = list.GetItems(qry).GetDataTable();
        return dataTable;
    }

    private void BindData(SPList list, string searchKeywords)
    {
         HyperLinkField detailLink = (HyperLinkField)(this.SPGridView1.Columns[0]);
         detailLink.DataNavigateUrlFormatString = editPageUrl + "?id={0}";

         this.AddNewItemLink.PostBackUrl = this.newPageUrl;

        DataTable listItems = SearchProperties(list);
        this.SPGridView1.DataSource = listItems;
        this.SPGridView1.DataBind();
    }

    protected void SPGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            
     
        }
    }
}


